package com.kaishengit.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	/**
	 * 用于获得当前的时间
	 * */
	public static String getNow() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
	/**
	 * 用于获得发表时间
	 * */
	public static String getAccount(String date1,String date2) throws ParseException{
		 long date_long1 = 0;
		 long date_long2 = 0;
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date_long1 = sdf.parse(date1).getTime();
			date_long2 =sdf.parse(date2).getTime();
	       
		} catch (ParseException e) {
			e.printStackTrace();
		}
		    long d=date_long1 - date_long2;
	        long n;
	        if(date_long1 > date_long2){
	        	if((n=d/1000/60/60/24)>=1){
	        		java.sql.Date dateTime=new java.sql.Date(sdf.parse(date2).getTime());
	        		return"   published "+  dateTime;
	        	}else if((n=d/ 1000 / 60 / 60 )>=1){
	        		return"   published "+n +" hours ago";
	        	}else if((n=d/ 1000 / 60  )>=1){
	        		return"   published "+n +" minutes ago";
	        	}else if((n=d/ 1000)>=10){
	        		return"   published "+n +" seconds ago";
	        	}else{
	        		return" published justnow ";
	        	}
	        }
	        return null;
	}
	public static String getNiceDate(String dateString)  {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = df.parse(dateString);
			
			long time = System.currentTimeMillis() - date.getTime();
			time = time / 1000;

			if(time < 10) {
				return " published justnow";
			} else if(time >= 10 && time < 60) {
				return "   published "+time+"seconds ago";
			} else if(time >= 60 && time < 3600) {
				return"   published "+time/60+"minutes ago";
			} else if(time >= 3600 && time < 86400) {
				return"   published "+time/3600+"hours ago";
			}else if(time>=86400 && time <86400*30){
				return "   published "+time/86400+"days ago";
			}else{
				java.sql.Date dateTime=new java.sql.Date(df.parse(dateString).getTime());
        		return"   published "+dateTime;
        	}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 * 用来判断任务的开始时间，结束时间与现在时间的关系
	 * */
	public static boolean isBefore(String date1,String date2){
		long date_long1 = 0;
		 long date_long2 = 0;
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 try {
			date_long1 = sdf.parse(date1).getTime();
			date_long2 =sdf.parse(date2).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 if( date_long1 > date_long2){
				 return true;
		 }
		 return false;
	}
	
	/**
	 * 用于判断评论，资料等是否还能修改删除(即发布时间与修改时间之间是否已超出5分钟）
	 * */
	public static String test(String date1,String date2) throws ParseException{
		long date_long1 = 0;
		 long date_long2 = 0;
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date_long1 = sdf.parse(date1).getTime();
			date_long2 = sdf.parse(date2).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 long d=date_long1 - date_long2;
		 if(d>300000){
			 return "0";
		 }
		return "1";
	}
	
	
}
